package com.faithbj.printer.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils
{

	private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

	public static String DateFormatDefault = "yyyy-MM-dd HH:mm:ss";
	public static String DateFormat = "yyyy-MM-dd";
	
	public static Date getAYearLater(String now)
	{
		String[] ss = now.split(" ");
		String[] ss1 = ss[1].split(":");
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.YEAR, +1);
		d.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss1[0]));
		d.set(Calendar.MINUTE, Integer.parseInt(ss1[1]));
		d.set(Calendar.SECOND, Integer.parseInt(ss1[2]));
		return d.getTime();
	}

	/**
	 * 获取几天前的时间
	 * 
	 * @param days
	 *            正数为几天前，反之为几天后
	 * @return
	 */
	public static Date getDaysAgo(int days)
	{
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.DATE, -days);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}

	public static Date dateAddDays(Date date, int addDays) {
		Date newDate = null;
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, addDays);
			newDate = cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return newDate;
	}
	
	/**
	 * 获得当前时间字符串
	 */
	public static Date getCurrentDate()
	{
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.DATE, 0);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}

	public static Date getNextYear(Date now)
	{
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.YEAR, +1);
		// d.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss1[0]));
		// d.set(Calendar.MINUTE, Integer.parseInt(ss1[1]));
		// d.set(Calendar.SECOND, Integer.parseInt(ss1[2]));
		return d.getTime();
	}

	public static String formateDate2String(String format, Date date)
	{
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String dateString = simpleDateFormat.format(date);
		return dateString;
	}

	/**
	 * 获得当前时间字符串，如果默认不填写格式就是yyyy-MM-dd HH:mm:ss
	 * 
	 * @param format
	 *            要返回的当前时间的格式串
	 */
	public static String getCurrentDateString(String format)
	{
		if (StringUtils.isBlank(format))
			format = "yyyy-MM-dd HH:mm:ss";
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		String dateString = formatter.format(currentTime);

		logger.debug(dateString);
		return dateString;
	}

	/**
	 * 把制定的时间按制定格式返回
	 * 
	 * @param time
	 *            时间
	 * @param formatString
	 *            格式
	 * @return 日期型
	 */
	public static Date getStringToDate(String time, String formatString)
	{

		DateFormat format = new SimpleDateFormat(formatString);
		Date d = null;
		try
		{
			d = format.parse(time);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return d;

	}

	/**
	 * 返回指定时间是星期几，星期日为1
	 * 
	 * @param date
	 * @return
	 */
	public static int getWeekOfDate(Date date)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek;
	}
}
